<template>
    <div class="login_container">
       <div class="login_box">
           <!-- 头像区域 -->
           <div class="avtar_box">
               <img src="../assets/logo.png" alt="">
           </div>
           <!-- 表单区域 -->
           <el-form :model="loginFom" :rules="rules" ref="loginFormRef" label-width="0px" class="login_form">
              <el-form-item prop="username">
                <el-input v-model="loginFom.username" prefix-icon="iconfont icon-user"></el-input>
              </el-form-item>
              <el-form-item prop="password">
                <el-input v-model="loginFom.password" prefix-icon="iconfont icon-3702mima"></el-input>
              </el-form-item>
              <el-form-item class="btn">
                 <el-button type="primary" @click="login">登录</el-button>
                 <el-button type="warning" @click="restLoginForm">重置</el-button>
              </el-form-item>

           </el-form>
       </div>
    </div>
</template>

<script>
export default {
  data () {
    return {
      loginFom: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入正确的密码', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    restLoginForm () {
      // 重置表单
      this.$refs.loginFormRef.resetFields()
    },
    login () {
      // 登录前表单经行预校验
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return false
        const { data: res } = await this.$http.post('login', this.loginFom)
        console.log(res)
        if (res.meta.status !== 200) return this.$message.error('登录失败')
        this.$message.success('登陆成功')
        // 登录成功后将token存储到本地
        window.sessionStorage.setItem('token', res.data.token)
        // 利用编程式导航跳转到home页面
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login_container{
    height: 100%;
    width: 100%;
    background-color: #6f7f8f;
    .login_box{
        width: 450px;
        height: 300px;
        border-radius: 3px;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        background-color: #fff;
        .avtar_box{
            width: 130px;
            height: 130px;
            border-radius: 50%;
            border: 1px solid #ccc;
            padding: 10px;
            position: absolute;
            top: 0;
            left: 50%;
            transform: translate(-50%,-50%);
            img{
                width: 100%;
                height: 100%;
                border-radius: 50%;
                background-color: #eee;
            }
        }
    }
}
.btn{
  display: flex;
  justify-content: flex-end;
}
.login_form{
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 20px;
  box-sizing: border-box;
}
</style>
